#!/bin/sh
# -----------------------------------------------------------------------------
# start/stop/restart/status Script for the ATXd
# -----------------------------------------------------------------------------
bc=/usr/bin/bc
ATX_HOME=/opt/atxd
JRE_HOME=/usr/lib/jvm/java-6-sun
APP_HOME="$ATX_HOME"/app
ATX_PID=/var/run/atxd.pid
PATH_LOG=/var/log/app
_RUNJSVC="$ATX_HOME"/bin/jsvc
APP_USER=appadmin
SERVICE_MONIT=/opt/admin/bin/servicemonit

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

if [ -r "$APP_HOME"/bin/setenv.sh ]; then
  . "$APP_HOME"/bin/setenv.sh
fi

if [ -r "$ATX_HOME"/bin/setclasspath.sh ]; then
	BASEDIR="$ATX_HOME"
	. "$ATX_HOME"/bin/setclasspath.sh
else
	echo "Cannot find $ATX_HOME/bin/setclasspath.sh"
	echo "This file is needed to run this program"
	exit 1
fi

# Build the classpath by using all the Jars in the /opt/aptx/app/bin
JAR_FILES=`ls "$APP_HOME"/bin/*.jar`
for file in $JAR_FILES
do
	CLASSPATH="$CLASSPATH":"$file"
done

if [ -z "$CATALINA_BASE" ] ; then
  CATALINA_BASE="$APP_HOME"
fi

if [ -z "$CATALINA_TMPDIR" ] ; then
  # Define the java.io.tmpdir to use for Catalina
  CATALINA_TMPDIR="$CATALINA_BASE"/temp
fi


# ----- Execute The Requested Command -----------------------------------------
getRunTimeVals () {
	Xms=64
	Xmx=256
    XPermGen=128
}

CLASSPATH="$CLASSPATH":"$ATX_HOME"/conf
start () {
	
	getRunTimeVals

	CATALINA_OPTS="-Xms"$Xms"m -Xmx"$Xmx"m -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize="$XPermGen"m "
	  
	"$_RUNJSVC" -user $APP_USER \
	        -home $JRE_HOME \
	        -jvm server \
	        -Dcatalina.home=$APP_HOME \
	        -Dcatalina.base=$CATALINA_BASE \
	        -Djava.io.tmpdir="$CATALINA_TMPDIR" \
	        -wait 10 \
	        -pidfile $ATX_PID \
	        -errfile '&1' \
	        -outfile $PATH_LOG/atxd.out \
	        $CATALINA_OPTS \
	      	-Dserver.home="$ATX_HOME" \
	      	-Dsun.net.inetaddr.ttl=0 \
	      	-Djava.util.logging.config.file="$ATX_HOME"/conf/logging.properties \
	      	-Djava.util.logging.manager=java.util.logging.LogManager  \
	      	-Dneprobe-config.dir="$ATX_HOME"/conf \
	      	-Dneprobe.home="$ATX_HOME" \
	      	-Djava.net.preferIPv4Stack=true \
	      	-Dhttp.nonProxyHosts="localhost|127.0.0.1" \
	        -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
	        -XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods \
            com.apptonelabs.atx.server.ATXProcess
	sleep 10
	chmod 775 $APP_HOME
	chmod -R 775 $PATH_LOG
}

# Stop the service
stop () {
  $ATX_HOME/bin/jsvc \
    -stop \
    -pidfile $ATX_PID \
    com.apptonelabs.atx.server.ATXProcess
}

# Display status of the service
status () {
        if [ -f $ATX_PID ]
        then
                PID=`cat $ATX_PID`
                result=`ps ax | grep $PID | grep -v 'grep' | awk '{print $1}'` > /dev/null 2>&1
                result=`echo $result | awk '{print $1}'`
                if [ "$result" = "$PID" ]
                then
                        echo "ATXd status = Running"
			exit 0
        else
                        echo "ATXd status = Stopped"
			exit 1
        fi
        else
                echo "ATXd status = Stopped"
		exit 1
        fi
}

# restart the service
restart () {
	stop
	sleep 5
	start
}

# Stop the service using Monit
mstop() {
	"$SERVICE_MONIT" atxd stop
}

# Start the service using Monit
mstart() {
	"$SERVICE_MONIT" atxd start
}

# Display status of the service using Monit
mstatus() {
	"$SERVICE_MONIT" atxd status
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  restart)
	restart
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|status|mstart|mstop|mstatus}"
        exit 1
esac